/**
 * Created by GYL on 2018/7/25
 */
import React, { Component } from 'react';
import { Card, Row, Col, Button, Input, Tag, message } from 'antd';
import style from './File.less';
import MyModal from '../../components/common/MyModal';
import Container from '../../components/common/Container';
import PublicService from '../../services/PublicService';
import MyTable from '../../components/common/MyTable';
import AddModal from './sub/AddModal';
import CheckModel from './sub/CheckModel';
import EditModel from './sub/EditModel';
import request from '../../utils/request';
import moment from 'moment'
import MyPagination from '../../components/common/MyPagination';
class File extends Component {
    state = {
        fileUploadList: [],
        total: 0,
        editModel:false,
        checkModel: false,
        addModal: false,
        loading: false,
        visible: false,
        selectedRowKeys: [],
        selectedRows: [],
        dataSource: [],
        pageIndex: 1,
        pageSize: 10,
        key: "",
        columns: [
            {
                title: '序号',
                dataIndex: 'num',
                width: '5%',
                align: "center",
            },
            {
                title: '燃气公司',
                dataIndex: 'name',
                width: '20%',
                align: "center",
            },
            {
                title: '统一社会信用代码',
                dataIndex: 'creditCode',
                width: '12%',
                align: "center",
            },
            {
                title: "法定代表人",
                dataIndex: 'legalRepresentative',
                width: '12%',
                align: "center",
                render: (text) => <span>{text ? text : '/'}</span>
            },
            {
                title: '公司地址',
                width: '20%',
                dataIndex: 'address',
                align: "center",
            },
            {
                title: '负责人',
                dataIndex: 'head',
                width: '10%',
                align: "center",
            },
            {
                title: '联系方式',
                dataIndex: 'phone',
                width: '12%',
                align: "center",
            },
            {
                title: '操作',
                dataIndex: 'userName',
                width: '17%',
                align: "center",
                render: (text, record) => {
                    return <span>
                        <Tag color="#108ee9" onClick={this.onEdit.bind(this, record)}>编辑</Tag>
                        <Tag color="#87d068" onClick={this.onCheck.bind(this, record)}>查看</Tag>
                    </span>

                }
            },

        ]
    }
    componentDidMount() {
        this.save(1, 10)
    }
    //编辑按钮
    onEdit = (record) => {
        console.log(record)
        request({
            url: `/zhrq-web/gas/company/getCompany?id=${record.id}`,
            method: 'GET',
        }).then(data => {
            console.log(data)
            if (data.rc == 0) {
                PublicService.setCookie('pid', record.id);
                data.ret.fileUploadList.forEach((item, index) => {
                    item.createTime = moment(item.createTime).format("YYYY-MM-DD HH:mm:ss")
                })
                this.setState({
                    creditCode:data.ret.creditCode,
                    name: data.ret.name,
                    createTime: data.ret.createTime,
                    head: data.ret.head,
                    legalRepresentative: data.ret.legalRepresentative,
                    phone: data.ret.phone,
                    fileUploadList: data.ret.fileUploadList,
                    editModel: true
                })
            }
        })
    }

    //查看按钮
    onCheck = (record) => {
        console.log(record)
        request({
            url: `/zhrq-web/gas/company/getCompany?id=${record.id}`,
            method: 'GET',
        }).then(data => {
            console.log(data)
            if (data.rc == 0) {
                data.ret.fileUploadList.forEach((item, index) => {
                    item.createTime = moment(item.createTime).format("YYYY-MM-DD HH:mm:ss")
                })
                this.setState({
                    creditCode:data.ret.creditCode,
                    name: data.ret.name,
                    createTime: data.ret.createTime,
                    head: data.ret.head,
                    legalRepresentative: data.ret.legalRepresentative,
                    phone: data.ret.phone,
                    fileUploadList: data.ret.fileUploadList,
                    checkModel: true
                })
            }
        })
    }

    //关键词搜索
    oninput = (e, b) => {

        this.setState({
            key: e.target.value
        })
    }

    onSelectChange = (keys, rows) => {
        console.log(rows)
        this.setState({
            selectedRowKeys: keys,
            selectedRows: rows
        });
    };

    //新增保存按钮
    ModalOk = (val, type, v) => {
        console.log(val, type, v)
        
        let data = {
            ...val,
            id: PublicService.getCookie('pid'),
            beenDeleted: 0
        }

        request({
            url: '/zhrq-web/gas/company/insertCompany',
            method: 'POST',
            data: data
        }).then(data => {
            console.log(data)
            if (data.rc == 0) {
                message.success("新增成功")
                this.save(1, 10)
                this.setState({
                    addModal: false,
                })
            } else {
                message.success("新增失败")
                this.save(1, 10)
                this.setState({
                    addModal: false,
                })
            }
        })


    }

    //编辑保存按钮
    editModelOk = (val, type, v) => {
        console.log(val, type, v)
        
        let data = {
            ...val,
            id: PublicService.getCookie('pid'),
            beenDeleted: 0
        }

        request({
            url: '/zhrq-web/gas/company/updateCompany',
            method: 'POST',
            data: data
        }).then(data => {
            console.log(data)
            if (data.rc == 0) {
                message.success("编辑成功")
                this.save(1, 10)
                this.setState({
                    editModel: false,
                })
            } else {
                message.success("编辑失败")
                this.save(1, 10)
                this.setState({
                    editModel: false,
                })
            }
        })


    }

    // 取消保存
    ModalCancel = (type) => {
        let t = this;
        t.setState({
            [type]: false,
            modalBtnLoading: false
        })
    };

    //新增
    addBtnText = () => {
        request({
            url: '/zhrq-web/gas/company/insertCompany',
            method: 'POST',
            data: { beenDeleted: 1 }
        }).then(data => {
            console.log(data)
            if (data.rc == 0) {
                PublicService.setCookie('pid', data.ret.id);
                this.setState({
                    btn: "保存",
                    addModal: true,
                });
            }
        })

    }

    //分页器
    onChangePage = (page, size) => {

        this.setState({
            page,
            rows: size,
            pageIndex: page,
            pageSize: size
        })
    };
    //查询
    save = (page, rows) => {
        let data = {
            page, rows,
            key: this.state.key
        }
        request({
            url: '/zhrq-web/gas/company/findPageList',
            method: 'POST',
            data: data
        }).then(data => {
            if (data.rc == 0) {
                this.setState({
                    dataSource: data.ret.items,
                    total: data.ret.rowCount
                })
            }
        })
    }

    //删除
    setDelConfirm = () => {
        let data = []
        this.state.selectedRows.forEach((item) => {
            data.push({
                id: item.id
            })
        })
        request({
            url: '/zhrq-web/gas/company/deleteList',
            method: 'POST',
            data: data
        }).then(data => {
            if (data.rc == 0) {
                message.success("删除成功")
                this.save(1, 10)
                this.setState({
                    selectedRowKeys: [],
                    selectedRows: [],
                })
            }
        })
    }

    render() {
        const { checkModel,editModel, total, loading, selectedRowKeys, dataSource, pageIndex, pageSize, addModal, modalBtnLoading } = this.state
        const windowWidth = document.body.clientWidth;

        const height = document.body.clientHeight;
        let containerHeight = '86vh';
        if (windowWidth > 1367) {
            containerHeight = '86vh';
        }
        let rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        };

        const items = [
            {
                type: 'input',
                label: "燃气公司名称",
                paramName: 'name',
                placeholder: '请输入',
                width: 200,
                span: 12
            },
            {
                type: 'input',
                label: "统一社会信用代码",
                paramName: 'creditCode',
                placeholder: '请输入',
                width: 200,
                span: 12
            },
            {
                type: 'input',
                label: '法定代表人',
                paramName: 'legalRepresentative',
                placeholder: '请输入',
                width: 200,
                span: 12
            },
            {
                type: 'input',
                label: '公司地址',
                paramName: 'address',
                placeholder: '请输入',
                width: 200,
                span: 12
            },
            {
                type: 'input',
                label: '负责人',
                paramName: 'head',
                placeholder: '请输入',
                width: 200,
                span: 12
            },
            {
                type: 'input',
                label: '联系方式',
                paramName: 'phone',
                placeholder: '请输入',
                width: 200,
                span: 12
            },
            {
                type: 'Upload',
                label: '相关附件',
                paramName: 'fileUploadId',
                width: 200,
                span: 12
            },
        ];
        const checkitems = [
            {
                type: 'input',
                label: "燃气公司名称",
                paramName: 'name',
                placeholder: '请输入',
                initialValue: this.state.name,
                disabled: true,
                width: 200,
                span: 12
            },
            {
                type: 'input',
                label: "统一社会信用代码",
                paramName: 'creditCode',
                placeholder: '请输入',
                initialValue: this.state.creditCode,
                disabled: true,
                width: 200,
                span: 12
            },
            {
                type: 'input',
                label: '法定代表人',
                paramName: 'legalRepresentative',
                initialValue: this.state.legalRepresentative,
                placeholder: '请输入',
                width: 200,
                disabled: true,
                span: 12
            },
            {
                type: 'input',
                label: '公司地址',
                paramName: 'address',
                initialValue: this.state.address,
                placeholder: '请输入',
                width: 200,
                disabled: true,
                span: 12
            },
            {
                type: 'input',
                label: '负责人',
                paramName: 'head',
                placeholder: '请输入',
                initialValue: this.state.head,
                width: 200,
                disabled: true,
                span: 12
            },
            {
                type: 'input',
                label: '联系方式',
                initialValue: this.state.phone,
                paramName: 'phone',
                placeholder: '请输入',
                width: 200,
                disabled: true,
                span: 12
            },
            {
                type: 'Upload',
                label: '相关附件',
                paramName: 'fileUploadId',
                width: 200,
                span: 12
            },
        ];
        return (
            <div>

                {
                    checkModel &&
                    <CheckModel
                        // type={type}
                        visible={checkModel}
                        title={"查看"}
                        style={{ height: "80vh" }}
                        // modalBtnLoading={modalBtnLoading}
                        data={checkitems}
                        fileUploadList={this.state.fileUploadList}
                        footerShow={false}
                        onModalSave={this.checkModel}
                        onCancel={this.ModalCancel.bind(this, 'checkModel')}

                    />
                }

                {
                    addModal &&
                    <AddModal
                        // type={type}
                        visible={addModal}
                        title={"新增"}
                        style={{ height: "80vh" }}
                        // modalBtnLoading={modalBtnLoading}
                        data={items}
                        footerShow={true}
                        onModalSave={this.ModalOk}
                        onCancel={this.ModalCancel.bind(this, 'addModal')}

                    />
                }
                {
                    editModel && 
                    <EditModel
                        // type={type}
                        visible={editModel}
                        title={"编辑"}
                        style={{ height: "80vh" }}
                        // modalBtnLoading={modalBtnLoading}
                        data={checkitems}
                        fileUploadList={this.state.fileUploadList}
                        footerShow={true}
                        onModalSave={this.editModelOk}
                        onCancel={this.ModalCancel.bind(this, 'editModel')}

                    />
                }
                <Card className={style.Top_box}
                    bodyStyle={{ padding: "6px 24px" }}
                >
                    <Row>
                        <Col span={12} ><span >关键词 : </span> <Input onChange={this.oninput} size="small" placeholder="请输入" style={{ width: 200, marginRight: 20, marginLeft: 5 }} value={this.state.textval} />  <Button type="primary" size="small" onClick={this.save.bind(this, 1, 10)}>查询</Button> <Button size="small">重置</Button></Col>
                    </Row>

                </Card>

                <Container
                    style={{ height: containerHeight, position: "relative", marginTop: "-1vh", padding: "0px 0px" }}
                    // header={true}
                    addBtnShow={true}
                    addBtnText={this.addBtnText}
                    setDelConfirm={this.setDelConfirm}
                    deleteBtnShow={true}
                >
                    <div style={{ height: "62vh", overflow: "auto" }}>
                        <MyTable
                            // bordered
                            loading={loading}
                            columns={this.state.columns}
                            rowSelection={rowSelection}
                            pagination={false}
                            // scroll={{ y: windowWidth < 1400 ? height - 280 : height - 300 }}
                            dataSource={PublicService.transformArrayData(dataSource, true, true, pageIndex, pageSize)}
                        />

                    </div>
                    <MyPagination

                        showPage
                        pageSize={pageSize}
                        current={pageIndex}
                        total={total}
                        showSizeChanger
                        showQuickJumper
                        onChange={this.onChangePage}
                        onShowSizeChange={this.onChangePage}
                    />

                </Container>
            </div>
        )
    }
}
export default (File); 
